<?php

namespace app\applet\controller;

use app\share\model\Token;
use app\share\model\WeChat;
use think\Db;
use think\Validate;

class Member
{
    public function __construct()
    {
        $this->member = new \app\applet\model\Member();
        $this->we_chat = new WeChat();
        $this->token = new Token();
    }
    #zlc-当前用户直接绑定微信手机号码
    public function bindPhoneWx()
    {
        $input = input();
        $validate = new Validate([
            ['encrypted_data', 'require', '加密数据不能为空'],
            ['iv', 'require', '加密算法的初始向量不能为空'],

        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $this->member->bindPhoneWx($input);
        returnJson(200,"手机号码绑定成功");
    }
    #zlc-当前用户绑定手机号码
    public function bindPhone()
    {
        $input = input();
        $validate = new Validate([
            ['phone', 'require', '手机号码不能为空'],
            ['code', 'require', '验证码不能为空'],

        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $this->member->bindPhone($input);
        returnJson(200,"success");

    }
    #zlc-查询当前用户是否绑定手机号码
    public function isPhone()
    {
        $data = $this->member->isPhone();
        returnJson(200,"success",$data);
    }
    //zlc-人脉名片行业分类
    public function industryList()
    {
        $data = $this->member->industryList();
        returnJson(200,"success",$data);
    }
    /**
     * 个人中心实名认证
     */
    public function realName()
    {
        $input = input();
        $validate = new Validate([
            ['real_name', 'require', '真实姓名不能为空'],
            ['card', 'require', '身份证号码不能为空'],
            ['phone', 'require', '电话号码不能为空'],
            ['code', 'require', '短信验证码不能为空'],
            ['card_just', 'require', '身份证正面不能为空'],
            ['card_back', 'require', '身份证反面不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $input['create_time'] = time();
        $input['status'] = 2;
        $input['member_id'] = get_header_id("token");
        $this->member->only($input['member_id']);
        $this->member->realName($input);
        returnJson(200,'提交资料成功');
    }

    /**
     *小程序登录接口
     * @param string code
     */
    public function login()
    {
        $input = input();
        $validate = new Validate([
            ['code', 'require', 'code不能为空'],
            ['nickName', 'require', '昵称不能为空'],
            ['avatarUrl', 'require', '头像不能为空'],
            ['gender', 'require', '性别不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        #获取小程序openid
        $auth_data = $this->we_chat->login($input['code']);
        #判断用户是否存在以及用户状态
        $status = $this->member->memberStatus($auth_data['openid']);
        if($status)
        {
            #更新会话秘钥
            Db::name("member")
                ->where(['openid'=>$auth_data['openid']])
                ->update(['session_key'=>$auth_data['session_key']]);

            $member_id = $status;
        }else{
            $auth_data['nickName'] = $input['nickName'];
            $auth_data['avatarUrl'] = $input['avatarUrl'];
            $auth_data['gender'] = $input['gender'];
            if(!empty($input['invt_code']))
            {
                $auth_data['father_code'] = $input['invt_code'];
            }
            $auth_data['create_time'] = time();
            #添加新用户数据
            $member_id = $this->member->memberAdd($auth_data);
            if(empty($member_id))
            {
                returnJson(-2004,'注册用户异常');
            }
        }
        #登录成功, 生成token,保持跨端登录
        $returnData['token'] = $this->token->createToken($member_id);
        $returnData['member_id'] = $member_id;

        returnJson(200,'success',$returnData);
    }

    /**
     * 个人中心人脉名片提交
     */
    public function contactCardPost(){
        $param=input();
        $validate = new Validate([
            ['avatarUrl', 'require', '头像不能为空'],
            ['real_name', 'require', '姓名不能为空'],
            ['gender', 'require', '性别不能为空'],
            ['birth_date', 'require', '出生年月不能为空'],
            ['social_idy', 'require', '社会身份不能为空'],
            ['company', 'require', '就职单位不能为空'],
            ['com_post', 'require', '职务不能为空'],
            ['industry', 'require', '行业不能为空'],
            ['address', 'require', '常住城市与地址不能为空'],
            ['signature', 'require', '个人特点不能为空'],
        ]);
        if(!$validate->check($param))
        {
            returnJson(-2003,$validate->getError());
        }
        $member_id = get_header_id("token");

        $this->member->contactCard($param,$member_id);


        returnJson(200,'发布成功');

    }

    /**
     * 个人中心人脉名片数据页面
     */
    public function contactCard(){
        $member_id = get_header_id("token");
        $returnData=$this->member->contactCardSelect($member_id);
        returnJson(200,'success',$returnData);
    }


    /**
     * 个人中心股东认证
     */
    public function partner(){
        $param=input();
        $validate = new Validate([
            ['company_name', 'require', '企业名称不能为空'],
            ['credit_code', 'require', '信用代码不能为空'],
            ['partner_name', 'require', '股东姓名不能为空'],
            ['partner_card', 'require', '身份证号不能为空'],
            ['business_license', 'require', '营业执照不能为空'],
        ]);
        if(!$validate->check($param))
        {
            returnJson(-2003,$validate->getError());
        }
        $member_id = get_header_id("token");
        $param['member_id']=$member_id;
        $param['create_time']=time();
        $param['status']=2;

        $this->member->checkPartner($member_id);

        $this->member->partner($param);

        returnJson(200,'提交资料成功');

    }

    /**
     * 意见反馈提交
     */
    public function feedback(){
        $param=input();
        $validate = new Validate([
            ['content', 'require', '反馈内容不能为空'],
        ]);
        if(!$validate->check($param))
        {
            returnJson(-2003,$validate->getError());
        }
        $member_id = get_header_id("token");
        $data['member_id']=$member_id;
        $data['create_time']=time();
        $data['content']=$param['content'];
        if(isset($param['img_list'])){
            $data['img_list']=$param['img_list'];

        }
        $this->member->feedback($data);

        returnJson(200,'提交成功');
    }

    /**
     * 意见反馈列表
     */
    public function feedbackList(){
        $page=empty($input['current_page']) ? 1 : $input['current_page'];
        $page_size=empty($input['page_num']) ? 10: $input['page_num'];
        $member_id = get_header_id("token");

        $returnData=$this->member->feedList($page,$page_size,$member_id);

        returnJson(200,'success',$returnData);
    }

    /**
     * 意见反馈详情
     */
    public function feedbackDetail(){

        $member_id = get_header_id("token");
        $id = input("f_id");

        $returnData=$this->member->feedDetail($member_id,$id);

        returnJson(200,'success',$returnData);
    }

    /**
     * 个人中心页面
     */
    public function personalCenter(){
        $member_id = get_header_id("token");

        $returnData=$this->member->pCenter($member_id);

        returnJson(200,'success',$returnData);

    }

    /**
     * 用户的默认地址或第一个收货地址
     */
    public function memberAddress(){
        $member_id = get_header_id("token");

        $returnData=$this->member->address($member_id);

        returnJson(200,'success',$returnData);

    }
    #商家收款最近3条记录
    public function collectionLog()
    {
        $data = $this->member->collectionLog();
        returnJson(200,"success",$data);
    }
    /**
     * 商家店铺二维码
     */
    public function business()
    {
        $member_id =get_header_id("token");
        $store = Db::name("store_apply")
            ->field("sa_id")
            ->where(['member_id'=>$member_id])
            ->find();
        $user = Db::name("member")
            ->field("invt_code")
            ->where(['member_id'=>$member_id])
            ->find();
        $inviteCode = $user['invt_code'];
        $store_id =$store['sa_id'];
        $path="pages/mall/business-home?id=$store_id&inviteCode=$inviteCode";
        $data = $this->we_chat->getBuiNssWxACode( $path);
        returnJson(200,'success',$data);
    }

    /**
     * 邀请新人
     */
    public function memberInvite(){
        $member_id =get_header_id("token");

        $inviteCode=$this->member->getInviteCode($member_id);
        $path="pages/aindex/index?invtCode=$inviteCode";
        $invt_qrcode = $this->we_chat->getWxACode( $path);
        $returnData['invt_url']=$path;
        $returnData['invt_code']=$inviteCode;
        $returnData['invt_qrcode']=$invt_qrcode;
        returnJson(200,'success',$returnData);
    }

    /**
     * 会员升级首页
     */
    public function memberGrade(){

        $member_id =get_header_id("token");

        $returnData=$this->member->memberGrade($member_id);

        returnJson(200,'success',$returnData);
    }

    /**
     * 会员升级
     */
    public function gradeHandle(){

        $input=input();
        $validate = new Validate([
            ['type', 'require', '推荐类型不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $member_id =get_header_id("token");
        $input['member_id']=$member_id;

        $this->member->gradeHandle($input);


    }

    /**
     * 取消支付
     */
    public function cancelPay(){
        $input = input();
        $validate = new Validate([
            ['order_sn', 'require', '支付单号不能为空'],

        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $input['member_id']= get_header_id("token");

        $returnData=$this->member->cancelOrder($input);
        returnJson(200,'success',$returnData);

    }
    /**
     * 我的推荐
     */
    public function myInviter(){
        $param=input();
        $validate = new Validate([
            ['type', 'require', '推荐类型不能为空'],
        ]);
        if(!$validate->check($param))
        {
            returnJson(-2003,$validate->getError());
        }
        $member_id =get_header_id("token");
        $current_page = empty($input['current_page']) ? 1 : $input['current_page'];
        $page_num = empty($input['page_num']) ? 10: $input['page_num'];
        $param['current_page']=$current_page;
        $param['page_num']=$page_num;
        $returnData=$this->member->getInviter($param,$member_id);

        returnJson(200,'success',$returnData);
    }

    /**
     * 奖金统计
     */
    public function awardCount(){
        $member_id =get_header_id("token");

        $returnData=$this->member->awardCount($member_id);

        returnJson(200,'success',$returnData);
    }


    /**
     * 会员设置
     */
    public function setting(){

    }

    /**
     * 我的招商页面
     */
    public function memberInvest(){
        $member_id =get_header_id("token");
        //$member_id=3;

        $returnData=$this->member->memberInvest($member_id);

        returnJson(200,'success',['mobile'=>$returnData]);
    }

    /**
     * 我的招商提交
     */
    public function investSubmit(){
        $param=input();
        $member_id =get_header_id("token");
        //$member_id=3;

        $validate = new Validate([
            ['invest_name', 'require', '乙方名称不能为空'],
            ['invest_code', 'require', '信用代码或身份证号不能为空'],
            ['invest_address', 'require', '地址不能为空'],
            ['msg_code', 'require', '验证码不能为空'],
        ]);
        if(!$validate->check($param))
        {
            returnJson(-2003,$validate->getError());
        }

        $this->member->investPost($param,$member_id);

        returnJson(200,'操作成功');
    }


}